package com.codeoss.framework.view.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.TableData;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.util.LookupInfo;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.IDataBinding;
import com.codeoss.framework.view.component.IDataType;
import com.codeoss.framework.view.component.ILookup;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.Table;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.model.SelectItem;

@Service
public class TableComponentUtil {
	@Autowired
	private PageBeanUtil pageBeanUtil;
	@Autowired
	private ITableStore tableStore;
	@Autowired
	private LookupUtil lookupUtil;
	public void setTableData(List<Map<String,Object>> data,String tableId) {
		YunContext yunContext = YunContext.getCurrentContext();
		Page page = yunContext.getPage();
		String newTableId=page.getCode()+"_"+tableId;
		page.findComponentById(yunContext, newTableId);
		Table tableComponent=(Table)yunContext.getFindComponent();
		List<IDataBinding> tableFieldList = new ArrayList<IDataBinding>();
		pageBeanUtil.getDataBindingList(tableComponent, tableFieldList);
		
		TableData tableData=new TableData();
		tableData.setData(data);
		tableData.setTotal(data.size());
		
		Map<String, List<SelectItem>> selectItemMap = new HashMap<String, List<SelectItem>>();
		yunContext.setAttribute(tableComponent.getId() + "_tabledata", tableData);
		yunContext.setAttribute(tableComponent.getId() + "_selectItemMap", selectItemMap);
		if(tableData.getData().size()==0) {
			return;
		}
		DataSourceConfig dataSourceConfig=page.getDataSourceConfigById(tableComponent.getDataSource());
		com.codeoss.framework.data.metadata.Table table=tableStore.get(dataSourceConfig.getTableName());
		for (int j = 0; j < tableFieldList.size(); j++) {
			IDataBinding dataBinding = tableFieldList.get(j);
			Component component = (Component) dataBinding;

			Column column = table.getColumnByColumnName(dataBinding.getDataField());
			((IDataType)dataBinding).setDataType(column.getDataType());
			if(dataBinding instanceof ILookup) {
				LookupInfo lookupInfo = ((ILookup)(dataBinding)).getLookupInfo();
				if (lookupInfo != null) {
					if(lookupInfo.getDataSourceConfig()!=null) {
						List<Map<String,Object>> dataList=tableData.getData();
						Set<Object> keyValueSet=new HashSet<Object>();
						for(int k=0;k<dataList.size();k++) {
							Object value=(Object)dataList.get(k).get(dataBinding.getDataField());
							if(value!=null) {
								keyValueSet.add(value);
							}
						}
						if(keyValueSet.size()==0) {
							continue;
						}
						List<SelectItem> selectItemList = lookupUtil.getSelectItemList(yunContext, lookupInfo,keyValueSet);
						if (selectItemList != null) {
							selectItemMap.put(component.getId(), selectItemList);
						}
					}else {
						List<SelectItem> selectItemList = lookupUtil.getSelectItemList(yunContext, lookupInfo);
						if (selectItemList != null) {
							selectItemMap.put(component.getId(), selectItemList);
						}
					}
				}
			}
		}
	}
}
